import { createRouter, createWebHashHistory } from 'vue-router'
import { getAuthToken } from '@/plugins/axios'
import NProgress from 'nprogress'

const whiteList = ['/login']
const childrenRoutes = [
  {
    path: '/',
    name: '驾驶舱',
    component: () => import('@/views/HomePage/index.vue')
  },
  {
    path: '/technology',
    name: '工艺总图',
    component: () => import('@/views/Technology/index.vue')
  },
  {
    path: '/information',
    name: '数据汇总',
    component: () => import('@/views/数据汇总/index.vue')
  },
  {
    path: '/operation',
    name: '综合运维',
    component: () => import('@/views/Operation/index.vue')
  },
  {
    path: '/analyse',
    name: '智能分析',
    component: () => import('@/views/Analyse/index.vue')
  },
  {
    path: '/security',
    name: '安防专题',
    component: () => import('@/views/Security/index.vue')
  },
  // {
  //   path: '/test',
  //   name: 'test',
  //   component: () => import('@/views/Information/index.vue')
  // }
]

const router = createRouter({
  history: createWebHashHistory(import.meta.env.BASE_URL),
  routes: [
    {
      path: '/login',
      name: 'login',
      component: () => import('@/views/LoginPage/index.vue')
    },
    {
      path: '/',
      name: 'layout',
      component: () => import('@/views/Layout/index.vue'),
      children: [
        ...childrenRoutes,
        {
          path: '/:pathMatch(.*)*',
          name: '404',
          component: () => import('@/views/DevPlaceholderPage/index.vue')
        }
        /**
         * name Mars3D
         */
        // {
        //   path: '/detail/:name/:menu',
        //   name: 'detail',
        //   component: () => import('@/views/DetailPage/index.vue')
        // }
      ]
    },
    {
      path: '/:pathMatch(.*)*',
      name: '404',
      component: () => import('@/views/DevPlaceholderPage/index.vue')
    }
  ]
})
router.beforeEach((to, from, next) => {
  NProgress.start()
  // next()
  // NProgress.done()
  if (getAuthToken()) {
    if (to.path === '/login') {
      next('/')
      NProgress.done()
    } else {
      next()
      NProgress.done()
    }
  } else {
    // 没有token
    if (whiteList.indexOf(to.path) !== -1) {
      // 在免登录白名单，直接进入
      next()
      NProgress.done()
    } else {
      next(`/login?redirect=${to.fullPath}`) // 否则全部重定向到登录页。
      // 但to.fullPath有可能为%2F，即http://172.16.6.205:9090/login?redirect=%2F，不带任何跳转路由。这时候，请看login.vue的跳转方法。
      NProgress.done()
    }
  }
})
export { childrenRoutes }
export default router
